package apps;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BallGraphics {

	/** default 960, 600 for inspiron screen */
	private static final int DEFAULT_WIDTH = 800;
	private static final int DEFAULT_HEIGHT = 800;

	/** Frame displaying selected image */
	private JFrame picFrame;
	private BufferedImage currentImage;

	public BallGraphics() {
		initializeGUI();
		currentImage = new BufferedImage(DEFAULT_WIDTH, DEFAULT_HEIGHT,
				BufferedImage.TYPE_INT_BGR);
	}

	private void initializeGUI() {
		picFrame = new JFrame("ImageView");
		picFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		picFrame.setSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
		picFrame.setVisible(true);
	}

	public void refresh() {
		picFrame.getContentPane().add(new DrawingPanel(currentImage));
		picFrame.repaint();
	}


	public void mandelbrot(double xMin, double xMax, double yMin, double yMax) {
		double x, y;
		double x0, y0;
		int iteration, max_iteration;
		double xtemp, ytemp;
		double xWid = xMax - xMin;
		double yHei = yMax - yMin;
		for (int xPixel = 0; xPixel < currentImage.getWidth(); xPixel++) {
			System.out.println("hi");
			for (int yPixel = 0; yPixel < currentImage.getHeight(); yPixel++) {
				x0 = (double) (xMin + (xWid * ((double) xPixel / (double) currentImage
						.getWidth())));
				y0 = (double) (yMin + (yHei * ((double) yPixel / (double) currentImage
						.getHeight())));
				x = x0;
				y = y0;
				iteration = 0;
				max_iteration = 10000;
				while (x * x + y * y < (2 * 2) && iteration < max_iteration) {
					xtemp = (x * x) - (y * y) + x0;
					ytemp = (2 * x * y) + y0;
					x = xtemp;
					y = ytemp;
					iteration++;
				}
				if (iteration == max_iteration) {
					currentImage.setRGB(xPixel, yPixel, 0);
				} else {
					// currentImage.setRGB(xPixel,yPixel, ((int)(0xFFFF)));
					currentImage
							.setRGB(
									xPixel,
									yPixel,
									((int) ((300.0 + (double) iteration * 255.0) / 1300.0)) << 16);
				}
			}
		}
		refresh();
	}

	class DrawingPanel extends JPanel {
		Image fImg = null;

		DrawingPanel(Image img) {
			fImg = img;
		}

		/** Display an image. * */
		public void paintComponent(Graphics g) {
			// First paint background
			super.paintComponent(g);
			int img_x = getSize().width / 2 - fImg.getWidth(this) / 2;
			int img_y = getSize().height / 2 - fImg.getHeight(this) / 2;
			g.drawImage(fImg, img_x, img_y, this);
		}
	}
	

	public static void main(String[] args) {
		// Schedule a job for the event-dispatching thread:
		// creating and showing this application's GUI.
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				BallGraphics b = new BallGraphics();
//				b.test();
				b.mandelbrot(-3, 3, -3, 3);
			}
		});
	}

}